# -*- coding: utf-8 -*-
'''
用于判断*手牌*是否胡牌了; 手上最多有14张牌(不含地上的牌)；用0把万，筒，条间隔
'''

class Hu(object):
    def __init__(self):
        self.huCombCode = set()
        config = "huComb.bin"
        fo = open(config, "r")        
        for line in fo.readlines():    #依次读取每行  
            line = line.strip()        # 删除空格与换行
            self.huCombCode.add(int(line, 10))

        # 关闭文件
        fo.close()
        #print(len(self.huCombCode))
    
    # 牌序，由小到大
    '''
    // 编码(万)：-0.1~-0.9
    // 编码(筒)：0.1~0.9 
    '''
    def CheckNormlHu(self, sorted_normal_tiles):   
        enc = self.NetEncode(sorted_normal_tiles)  
        print(enc)
        return enc in self.huCombCode

    # 牌序，由小到大
    '''
    // 编码(万)：0x01~0x09 
    // 编码(筒)：0x11~0x19 
    // 编码(条)：0x21~0x29 
    '''
    def CheckHu(self, sorted_tiles):   
        enc = self.Encode(sorted_tiles)   
        print(enc)
        return enc in self.huCombCode


    # 规范化值值转换(仅万， 筒)
    '''
    // 编码(万)：-0.1~-0.9 
    // 编码(筒)：0.1~0.9 
    '''
    def NetEncode(self, sorted_normal_tiles):
        s = ""
        s1 = ""
        s2 = ""

        #补0
        for t in sorted_normal_tiles:
            if t < 0:
                s = chr(int(t*(-10)) + 48) + s  # 万
            else:
                s1 = s1 + chr(int(t * 10) + 48)   # 筒

        if s != "":
            s = s + "0"
        s = s + s1
        if s != "":
            s = s + "0"
        s = s + s2

        print("sss:", s, ":", sorted_normal_tiles)
        return  int(s, 10)

    '''
    // 编码(万)：0x01~0x09 
    // 编码(筒)：0x11~0x19 
    // 编码(条)：0x21~0x29 
    '''
    def Encode(self, sorted_tiles):
        s = ""
        s1 = ""  #筒间隔
        s2 = ""
        #补0
        for t in sorted_tiles:
            if t < 10:
                s = s + chr(t + 48) # 万
            elif t < 26:  # 0x1a:
                s1 = s1 + chr(t + 32) # 筒
            else:
                s2 = s2 + chr(t + 16)  # 筒

        if s != "":
            s = s + "0"
        s = s + s1
        if s != "":
            s = s + "0"
        s = s + s2
        #print("sss:", s, ":", sorted_normal_tiles)
        return  int(s, 10)

    def TestNetEncode(self):
        hu = [0.1, 0.1]  # 一对1万
        print(self.CheckNormlHu(hu),hu)
        hu = [0.1, 0.2]  # 1,2万
        print(self.CheckNormlHu(hu), hu)
        hu = [0.1, 0.2, 0.3, -0.3, -0.3] 
        print(self.CheckNormlHu(hu), hu)
        hu = [0.1, 0.2, 0.3, -0.4, -0.4] 
        print(self.CheckNormlHu(hu), hu)
        hu = [-0.5, -0.4, -0.3, -0.1, -0.1] 
        print(self.CheckNormlHu(hu), hu)

    def TestEncode(self):
        hu = [0x01, 0x01]  # 一对1万
        print(self.CheckHu(hu),hu)
        hu = [0x01, 0x02]  # 1,2万
        print(self.CheckHu(hu), hu)
        hu = [0x01, 0x02, 0x03, 0x11, 0x11]
        print(self.CheckHu(hu), hu)
        hu = [0x01, 0x02, 0x03, 0x11, 0x11, 0x23, 0x23, 0x23] 
        print(self.CheckHu(hu), hu)
Hu().TestNetEncode()
Hu().TestEncode()
#Hu().TestMillion()
